package com.gbiac.modules.monitor.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gbiac.modules.monitor.entity.Seat;
import com.gbiac.modules.monitor.mapper.AttendanceReportMapper;
import com.gbiac.modules.monitor.model.query.AttendanceReportQuery;
import com.gbiac.modules.monitor.model.vo.AttendanceReportSeatVO;
import com.gbiac.modules.monitor.service.AttendanceReportService;
import com.gbiac.modules.monitor.service.ISeatService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 考勤报表服务
 * @Author: fengchengguo
 * @Date:   2024-11-30
 * @Version: V1.0
 */
@Service
public class AttendanceReportServiceImpl implements AttendanceReportService {

    @Resource
    private AttendanceReportMapper attendanceReportMapper;
    @Resource
    private ISeatService iSeatService;


    @Override
    public Page<JSONObject> queryUserReport(AttendanceReportQuery attendanceReportQuery, Integer pageNo, Integer pageSize) {
        Integer dateType = attendanceReportQuery.getDateType();
        List<Long> timeList = getTimeList(dateType, attendanceReportQuery.getQueryDate());
        String dateField="day";
        String groupFormat = "%d";
        String format = "%Y-%m";
        if(attendanceReportQuery.getDateType() == 2){
            dateField="month";
            groupFormat="%m";
            format="%Y";
        }
        attendanceReportQuery.setTimeList(timeList);
        attendanceReportQuery.setFormat(format);
        attendanceReportQuery.setGroupFormat(groupFormat);
        attendanceReportQuery.setDateField(dateField);

        Page<JSONObject> pageResult = attendanceReportMapper.queryUserReport(new Page(pageNo,pageSize),attendanceReportQuery);

        List<JSONObject> records = pageResult.getRecords();
        if(CollectionUtil.isNotEmpty(records)){
            for (int i = 0; i < records.size(); i++) {
                JSONObject json = records.get(i);
                String id = json.getString("id");
                Integer sumCount = json.getInteger("sumCount");
                if(sumCount > 0){
                    List<JSONObject> list = attendanceReportMapper.queryUserReportDetail(id,attendanceReportQuery);
                    if(CollectionUtil.isNotEmpty(list)){
                        list.stream().forEach(item->{
                            String string = item.getString("fieldName");
                            Double num = item.getDouble("num");
                            json.put(string,num);
                        });
                    }
                }else {
                    for (int i1 = 0; i1 < timeList.size(); i1++) {
                        json.put(dateField+timeList.get(i1),0);
                    }
                }
            }
        }
        return pageResult;
    }

    @Override
    public Page<JSONObject> querySeatReport(AttendanceReportQuery attendanceReportQuery, Integer pageNo, Integer pageSize) {

        Integer dateType = attendanceReportQuery.getDateType();
        String format = "%Y-%m";
        if(dateType == 2){
            format="%Y";
        }
        attendanceReportQuery.setFormat(format);
        List<Seat> seats = iSeatService.list();

        Page<JSONObject> pageResult = attendanceReportMapper.querySeatReport(new Page(pageNo,pageSize),attendanceReportQuery);

        List<JSONObject> records = pageResult.getRecords();
        if(CollectionUtil.isNotEmpty(records)){
            for (int i = 0; i < records.size(); i++) {
                JSONObject json = records.get(i);
                String id = json.getString("id");
                Integer sumCount = json.getInteger("sumCount");
                if(sumCount > 0){
                    List<JSONObject> list = attendanceReportMapper.querySeatReportDetail(id,attendanceReportQuery,seats);
                    if(CollectionUtil.isNotEmpty(list)){
                        list.stream().forEach(item->{
                            String string = item.getString("fieldName");
                            Double num = item.getDouble("num");
                            json.put(string,num);
                        });
                    }
                }else {
                    for (int i1 = 0; i1 < seats.size(); i1++) {
                        json.put(seats.get(i1).getName(),0);
                    }
                }
            }
        }
        return pageResult;
    }

    public static List<Long> getTimeList(Integer dateType,Date date){
        LocalDate localDate =  date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        List<Long> timeList = new ArrayList<>();
        if(dateType==null || dateType==1){//月份
            LocalDate localDate2 = localDate.plusMonths(1);
            long daysBetween = ChronoUnit.DAYS.between(localDate, localDate2);
            for (long i = 1; i <= daysBetween; i++) {
                timeList.add(i);
            }
        }else { //年份
            LocalDate localDate2 = localDate.plusYears(1);
            long daysBetween = ChronoUnit.MONTHS.between(localDate, localDate2);
            for (long i = 1; i <= daysBetween; i++) {
                timeList.add(i);
            }
        }
        return timeList;
    }
}
